package org.eerbaar.lang.expressions;

import org.eerbaar.lang.Expression;
import org.eerbaar.lang.HasRuntime;
import org.eerbaar.lang.Values;
import org.eerbaar.lang.runtime.Executor;

public class Equal implements Expression, HasRuntime {

    private Expression e1;
    private Expression e2;

    public Equal(Expression e1, Expression e2) {
        this.e1 = e1;
        this.e2 = e2;
    }

    public org.eerbaar.lang.runtime.Runtime getRuntime() {

        return new org.eerbaar.lang.runtime.Runtime() {

            private Object result = Values.UNDEF;

            @Override
            public void execute(Executor exec) {
                exec.execute(e1);
                Object b1 = exec.getResult();
                exec.execute(e2);
                Object b2 = exec.getResult();
                if (b1.equals(b2)) {
                    result = Values.TRUE;
                } else {
                    result = Values.FALSE;
                }
            }

            @Override
            public Object getResult() {
                return result;
            }
        };
    }

}
